package com.gerfmarquez.nymph.ui.threads;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;

import com.gerfmarquez.nymph.R;
import com.gerfmarquez.nymph.ui.seekbar.VerticalSeekBar;

public class UpdatePlayStateNotesThread extends Thread {
	Context context;
	ArrayList<VerticalSeekBar> verticalSeekBars;
	int []bytesPerNoteAccumulated;
	int currentNote = 0;
	Resources r;
	private static String TAG = "UpdatePlayStateNotesThread";
	Handler uiHandler;
	
	static Drawable prDrInactive;
	static Drawable prDrActive;
	
	boolean allViewsProcessed;
	
	public void setBytesPerNote(int[] bytesPerNote) {
		this.bytesPerNoteAccumulated = bytesPerNote;
	}
	public UpdatePlayStateNotesThread(Context context,ArrayList<VerticalSeekBar> verticalSeekBars, Handler handler) {
		this.uiHandler = handler;
		this.context = context;
		this.verticalSeekBars = verticalSeekBars;
		r = context.getResources();
		prDrActive = r.getDrawable(R.drawable.myprogress);
	}
	@Override
	public void run() {
		while(!allViewsProcessed) {
			try {
				Thread.sleep(500);
			} catch(InterruptedException ie) {
				
			}
		}
		

	}
	public void changeToPlayState(final int currentBytes) {
		for(int i = 0;i<bytesPerNoteAccumulated.length;i++) {
			if(currentBytes == bytesPerNoteAccumulated[i]) {
				
				uiHandler.post(new Runnable() {
					VerticalSeekBar vs;
					Rect bounds;
					int progr;
					int max;
					int width;
					int height;
					 {
						vs = verticalSeekBars.get(currentNote);
						bounds = vs.getProgressDrawable().getBounds();
						progr = vs.getProgress();
						max = vs.getMax();
						width = vs.getWidth(); 
						height = vs.getHeight();
					}
					
					public void run() {
						if(currentBytes != 0) {
							VerticalSeekBar vsBack = verticalSeekBars.get(currentNote-1);
							Rect bounds = vsBack.getProgressDrawable().getBounds();
							int progr = vsBack.getProgress();
							int max = vsBack.getMax();
							int width = vsBack.getWidth(); 
							int height = vsBack.getHeight();
							vsBack.setProgressDrawable(prDrInactive);
							vsBack.getProgressDrawable().setBounds(bounds);
							vsBack.setMax(max);
							vsBack.setProgress(1);
							vsBack.setProgress(progr);
							vsBack.onSizeChanged(width, height);
							vsBack.refreshDrawableState();
						}
						//Delay that makes synchronization of painting note.
//						try {
//							Thread.sleep(300);
//						} catch(InterruptedException ie) {
//						}
						prDrInactive = vs.getProgressDrawable();
						
						vs.setProgressDrawable(prDrActive);
						vs.getProgressDrawable().setBounds(bounds);
						vs.setMax(max);
						vs.setProgress(1);
						vs.setProgress(progr);
						vs.onSizeChanged(width, height);
						vs.refreshDrawableState();
					}
				});
				
				if(currentNote < verticalSeekBars.size()-1)
					currentNote++;

			}
		}
		
	}
	
	
}
